<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Forpastime < forpastime@qq.com>
// +----------------------------------------------------------------------
namespace app\tenant\controller;

use app\tenant\model\TenantCategoryModel;
use cmf\controller\AdminBaseController;
use think\Db;
use tree\Tree;

class AdminCategoryController extends AdminBaseController
{

    /**
     * 商户分类管理
     * @adminMenu(
     *     'name'   => '商户分类',
     *     'parent' => 'tenant/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '商户分类',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $tenantCategoryModel = new TenantCategoryModel();
        $categories        = $tenantCategoryModel->getTenantCategories();
        $this->assign('categories', $categories);
        return $this->fetch();
    }

    /**
     * 商户分类 添加
     * @adminMenu(
     *     'name'   => '添加商户分类',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '添加商户分类',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function add()
    {
        $parentId = $this->request->param('parent',0,'intval');
        
        $tenantCategoryModel = new TenantCategoryModel();
        // $categoriesTree    = $tenantCategoryModel->mallCategoryTree($parentId);
        // $this->assign('categories',$categories);

        $categories = $tenantCategoryModel->tenantCategoryTree($parentId);
        $this->assign('categories_tree',$categories);

        return $this->fetch();
    }

    /**
     * 商户分类 添加提交
     * @adminMenu(
     *     'name'   => '添加商户分类提交',
     *     'parent' => 'add',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '添加商户分类提交',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function addPost()
    {
        $param = $this->request->param();
        $res = $this->validate($param,'TenantCategory');

        if($res !== true){
            return $this->error($res);
        }

        $tenantCategoryModel = new TenantCategoryModel();
        $res = $tenantCategoryModel->addCategory($param);
        if($res){
            $this->success('新建分类成功', 'index');
        }else{
            $this->error('新建分类失败');

        }
    }

    /**
     * 编辑分类
     * @adminMenu(
     *     'name'   => '编辑商户分类',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '编辑商户分类',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $id = $this->request->param('id', 0, 'intval');
        if (empty($id)) {
            $this->error('操作错误!');
        }
        $tenantCategoryModel = new TenantCategoryModel();
        $category          = $tenantCategoryModel::get($id)->toArray();
        $categoriesTree    = $tenantCategoryModel->tenantCategoryTree($category['parent_id'], $id);

        $this->assign('category', $category);
        $this->assign('categories_tree', $categoriesTree);
        return $this->fetch();

    }

    /**
     * 编辑商户分类提交
     * @adminMenu(
     *     'name'   => '编辑商户分类提交',
     *     'parent' => 'edit',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '编辑商户分类提交',
     *     'param'  => ''
     * )
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editPost()
    {
        $data   = $this->request->param();
        $result = $this->validate($data, 'TenantCategory');

        if ($result !== true) {
            $this->error($result);
        }

        $tenantCategoryModel = new TenantCategoryModel();
        $result            = $tenantCategoryModel->editCategory($data);
        if ($result) {
            $this->success('编辑商户分类成功');
        } else {
            $this->error('编辑商户分类失败');
        }
    }

        /**
     * 设置商户分类启用状态
     * @adminMenu(
     *     'name'   => '设置商户分类启用状态',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '设置商户分类启用状态',
     *     'param'  => ''
     * )
     */
    public function setStatusOn()
    {
        $id = $this->request->param('id', 0, 'intval');
        if (empty($id)) {
            $this->error('状态更改操作失败');
        }
        $tenantCategoryModel = new TenantCategoryModel();
        $result            = $tenantCategoryModel->where('id', $id)->update(['status' => 1]);
        if ($result) {
            $this->success('状态更改操作成功');
        } else {
            $this->error('状态更改操作失败');
        }

    }

    /**
     * 设置商户分类禁用状态
     * @adminMenu(
     *     'name'   => '设置商户分类禁用状态',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '设置商户分类禁用状态',
     *     'param'  => ''
     * )
     */
    public function setStatusOff()
    {
        $id = $this->request->param('id', 0, 'intval');
        if (empty($id)) {
            $this->error('状态更改操作失败');
        }
        $tenantCategoryModel = new tenantCategoryModel();
        $result            = $tenantCategoryModel->where('id', $id)->update(['status' => 0]);
        if ($result) {
            $this->success('状态更改操作成功');
        } else {
            $this->error('状态更改操作失败');
        }
    }

    /**
     *删除商户分类
     * @adminMenu(
     *     'name'   => '删除商户分类',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除商户分类',
     *     'param'  => ''
     * )
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        $id = $this->request->param('id', 0, 'intval');
        if (empty($id)) {
            $this->error('意外的错误');
        }
        $tenantCategoryModel = new tenantCategoryModel();
        //获取删除的内容
        $findCategory = $tenantCategoryModel->where('id', $id)->find();

        if (empty($findCategory)) {
            $this->error('分类不存在!');
        }
        //判断此分类有无子分类（不算被删除的子分类）
        $categoryChildrenCount = $tenantCategoryModel
            ->where('parent_id', $id)
            ->where('delete_time', 0)
            ->count();

        if ($categoryChildrenCount > 0) {
            $this->error('此分类有子类无法删除!');
        }

        $result = $tenantCategoryModel
            ->where('id', $id)
            ->delete();
        if ($result) {
            $this->success('删除成功!');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * 商户分类排序
     * @adminMenu(
     *     'name'   => '商户分类排序',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '商户分类排序',
     *     'param'  => ''
     * )
     */
    public function listOrder()
    {
        $tenantCategoryModel = new tenantCategoryModel();
        parent::listOrders($tenantCategoryModel);
        $this->success("排序更新成功！");
    }
}